package com.example.linearlayoutdemo.network.auth.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.example.linearlayoutdemo.network.auth.model.User;
import com.example.linearlayoutdemo.network.auth.repository.AuthRepository;

/**
 * 主界面ViewModel，处理登录后的业务逻辑
 */
public class MainViewModel extends AndroidViewModel {
    private final AuthRepository authRepository;
    public final MutableLiveData<User> currentUser = new MutableLiveData<>();
    public final MutableLiveData<Boolean> isLoggedOut = new MutableLiveData<>();

    public MainViewModel(@NonNull Application application) {
        super(application);
        authRepository = AuthRepository.getInstance(application);
        loadCurrentUser();
    }

    /**
     * 加载当前登录用户信息
     */
    public void loadCurrentUser() {
        User user = authRepository.getCurrentUser();
        currentUser.setValue(user);
    }

    /**
     * 退出登录
     */
    public void logout() {
        authRepository.logout();
        isLoggedOut.setValue(true);
    }
}

